package com.example.a2209_shop.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.GoodsEntity
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class SearchViewModel:BaseViewModel() {
    val searchSuccess = MutableLiveData<List<GoodsEntity>>()
    val searchFail = MutableLiveData<String>()

    fun search(name:String){
        viewModelScope.launch {
            try {
                val goods = apiServer.goods(0, 1, 200)

                val data = goods.data.filter {
                    it.goods_desc.contains(name)
                }
                searchSuccess.value = data
            }catch (e:Exception){
                searchFail.value = e.message
            }
        }
    }
}